var validator;
var $meetingRoomEditForm = $("#meetingroom-edit-form");

$(function() {
    meetingroomValidateRule();

        $("input[name='licensed']").change(function () {
            var checked = $(this).is(":checked");
            var $status_label = $("#licensed");
            // chaohung , 20190114 
            /*if (checked) $status_label.html('已授权');
            else $status_label.html('未授权');*/
            var meetingroomlicensetext = $MB.i18n('meetingroom.licensetext'),
            meetingroomunlicensetext = $MB.i18n('meetingroom.unlicensetext');
            if (checked) $status_label.html(meetingroomlicensetext );
            else $status_label.html(meetingroomunlicensetext);
        });
    //Tpv zhongxiang.huang 20181228, modify btn-save to action-save
    $("#meetingroom-edit .action-save").click(function() {
        var validator = $meetingRoomEditForm.validate();
        var flag = validator.form();
        if (flag) {
            $.post(ctx + "meetingroom/update", $meetingRoomEditForm.serialize(), function(r) {
                if (r.code === 0) {
                    closemeetingRoomModal();
                    $MB.n_success(r.msg);
                    $MB.refreshTable("meetingRoomTable");
                } else $MB.n_danger(r.msg);
            });
        }
    });

    $("#meetingroom-edit .btn-close").click(function() {
        closemeetingRoomModal();
    });
    //Tpv zhongxiang.huang 20181228, add meetingroom upgrade
    $("#meetingroom-edit .action-meetingroom-upgrade").click(function() {
    	$MB.closeModal("meetingroom-edit");
    	openMeetingroomUpgradeGuide($("#meetingroom-edit").find("input[name='hardwareKey']").val());
    });
});

function closemeetingRoomModal() {
    $MB.closeAndRestModal("meetingroom-edit");
}

function meetingroomValidateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    
    // chaohung , 20190114 
    var meetingroomValidateRulemeetingRoomName = $MB.i18n('meetingroom.ValidateRule.meetingRoomName');
    var meetingroomValidateRuleminlength = $MB.i18n('meetingroom.ValidateRule.minlength');
    var meetingroomlocationminlength = $MB.i18n('meetingroom.location.minlength');
    var meetingroommaxUsersAllowedrequired = $MB.i18n('meetingroom.maxUsersAllowed.required');
    var meetingroomsetexcessspacedigits = $MB.i18n('meetingroom.setexcessspace.digits');
    var meetingroommaxUsersAllowedrange = $MB.i18n('meetingroom.maxUsersAllowed.range');
    var meetingroomsetexcessspacerequired = $MB.i18n('meetingroom.setexcessspace.required');
    var meetingroomsetexcessspacerange = $MB.i18n('meetingroom.setexcessspace.range');
    
        		   
    validator = $meetingRoomEditForm.validate({
        rules: {
            meetingRoomName: {
                required: true,
                minlength: 3,
                maxlength: 20
                    //                remote: {
                    //                    url: "user/checkUserName",
                    //                    type: "get",
                    //                    dataType: "json",
                    //                    data: {
                    //                        username: function () {
                    //                            return $(".meeting-join.input[name='username']").val().trim();
                    //                        },
                    //                        oldusername: function () {
                    //                            return $("input[name='oldusername']").val().trim();
                    //                        }
                    //                    }
                    //                }
            },
            location: {
               minlength: 1,
               maxlength: 30
            },
            maxUsersAllowed: {
                required: true,
                digits:true,
                range:[2,30]
            },
            // chaohung , 20190114 
            setexcessspaceshow: {
                required: true,
                digits:true,
                range:[1,10000]
            }
        },
        errorPlacement: function(error, element) {
            if (element.is(":checkbox") || element.is(":radio")) {
                error.appendTo(element.parent().parent());
            } else {
                error.insertAfter(element);
            }
        },
        messages: {
            meetingRoomName: {
            	// chaohung , 20190114 
                /*required: icon + "请输入会议室名",
                minlength: icon + "会议室名长度3到20个字符"*/
            	required: icon + meetingroomValidateRulemeetingRoomName,
                minlength: icon + meetingroomValidateRuleminlength
            },
            location: {
            	// chaohung , 20190114 
                //minlength: icon + "内容长度1到10个字符"
            	minlength: icon + meetingroomlocationminlength
            },
            maxUsersAllowed: {
            	// chaohung , 20190114 
                /*required: icon + "请输入并发用户数",
                digits: icon + "要求数字",
                range: icon + "请输入范围在 2-30 之间的数值"*/
            	required: icon + meetingroommaxUsersAllowedrequired,
                digits: icon + meetingroomsetexcessspacedigits,
                range: icon + meetingroommaxUsersAllowedrange
            },
            // chaohung , 20190114
            setexcessspaceshow: {
                /*required: icon + "请输入 会议室超出空间配额",
                digits: icon + "要求数字",
                range: icon + "请输入范围在 1-10000 之间的数值"*/
            	required: icon + meetingroomsetexcessspacerequired,
                digits: icon + meetingroomsetexcessspacedigits,
                range: icon + meetingroomsetexcessspacerange
            }
        }
    });
}